import Vue from 'vue'
import moment from 'moment';
// import parseTime, formatTime and set to filter
export { parseTime, formatTime } from '@/utils'

/**
 * Show plural label if time is plural number
 * @param {number} time
 * @param {string} label
 * @return {string}
 */
function pluralize(time, label) {
  if (time === 1) {
    return time + label
  }
  return time + label + 's'
}

/**
 * @param {number} time
 */
export function timeAgo(time) {
  const between = Date.now() / 1000 - Number(time)
  if (between < 3600) {
    return pluralize(~~(between / 60), ' minute')
  } else if (between < 86400) {
    return pluralize(~~(between / 3600), ' hour')
  } else {
    return pluralize(~~(between / 86400), ' day')
  }
}

/**
 * Number formatting
 * like 10000 => 10k
 * @param {number} num
 * @param {number} digits
 */
export function numberFormatter(num, digits) {
  const si = [
    { value: 1E18, symbol: 'E' },
    { value: 1E15, symbol: 'P' },
    { value: 1E12, symbol: 'T' },
    { value: 1E9, symbol: 'G' },
    { value: 1E6, symbol: 'M' },
    { value: 1E3, symbol: 'k' }
  ]
  for (let i = 0; i < si.length; i++) {
    if (num >= si[i].value) {
      return (num / si[i].value).toFixed(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + si[i].symbol
    }
  }
  return num.toString()
}

/**
 * 10000 => "10,000"
 * @param {number} num
 */
export function toThousandFilter(num) {
  return (+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
}

/**
 * Upper case first char
 * @param {String} string
 */
export function uppercaseFirst(string) {
  return string.charAt(0).toUpperCase() + string.slice(1)
}


Vue.filter("str", (val) => {
  if (!val) return "";
  return val.toString()
})

Vue.filter("fromNow", (t) => {
  if (!t) return "";
  let r = moment(t).fromNow()
  if(r==='几秒后'||r==='几秒前'){
    r = '刚刚'
  }
  return r
})
Vue.filter("fmtDate", (val) => {
  if (!val) return "";
  return val.substr(0, 10)
})
Vue.filter("fmtHour", (val) => {
  if (!val) return "";
  return val.substr(0, 13)
})
Vue.filter("fmtMinute", (val) => {
  if (!val) return "";
  return val.substr(0, 16)
})
Vue.filter("genderTitle", (val) => {
  return val ? "男" : (val === undefined ? "" : "女")
})

Vue.filter("yesNoFilter", (val) => {
  return val ? "是" : (val === undefined ? "" : "否")
})

const userStatus = {
  0: "正常",
  2: "停用",
  9: "删除",
}
Vue.filter("userStatus", (val) => {
  return userStatus[val] || "";
})

Vue.filter("username", (e) => {
  return e.realname || e.username || '';
})

Vue.filter("roleTitles", (arr) => {
  arr = arr || []
  return arr.map(e => {
    return e.title || e
  }).join()
})

Vue.filter("roleStatusFilter", (role) => {
  if (role.deleted) {
    return "删除"
  } else if (role.disabled) {
    return "停用"
  } else {
    return "正常"
  }
})

Vue.filter("userAvatar", (user) => {
  // let avatar = (user.avatar||`/dat/zz/avatar/default.gif`);
  return user.url_base ? `${user.url_base}${user.avatar}` : user.avatar;
})
Vue.filter("userAvatarFilter", (user) => {
  // let avatar = (user.avatar||`/dat/zz/avatar/default.gif`);
  return user.url_base ? `${user.url_base}${user.avatar}` : user.avatar;
})
Vue.filter("getFilename", (uri) => {
  uri = uri || '';
  let pos = uri.lastIndexOf("/");
  if (pos < 0) {
    pos = uri.lastIndexOf("\\");
  }
  return (pos > 0)?uri.substring(pos + 1):uri;
})

Vue.filter("filesize", (size) => {
  if(!size) return '';
  size = size/1024;
  let unit = "KB"
  if(size>=1024){
    size = size/1024;
    unit = "MB"
  }
  size = size.toFixed(1)
  return `【${size}${unit}】`
})
